package gson.emlog.activity;

import gson.emlog.R;
import gson.emlog.adapter.CommentsAdapter;
import gson.emlog.utils.Action;
import gson.emlog.utils.Http;
import gson.emlog.utils.Result;

import java.util.HashMap;

import org.json.simple.JSONArray;
import org.json.simple.JSONObject;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.res.Resources;
import android.os.Bundle;
import android.os.Handler;
import android.text.InputType;
import android.view.Gravity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast;

public class Comments extends Activity {

	private Context context;
	ListView lv;
	private long bid;
	private long pid;
	private String hide;
	private CommentsAdapter cadpt;
	private JSONArray data;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		context = this;
		setContentView(R.layout.comment);

		lv = (ListView) findViewById(R.id.commentView);
		Toast.makeText(Comments.this, "加载中ing", Toast.LENGTH_SHORT).show();
		Handler handler = new Handler();
		Runnable runnable = new Runnable() {
			public void run() {
				Http http = new Http(Comments.this);
				Result obj = http.post(Action.BLOG_COMMENT);
				data = new JSONArray();
				if (obj.getStatus()) {
					data = (JSONArray) obj.getData();
				} else {
					Toast.makeText(Comments.this, obj.getData().toString(),
							Toast.LENGTH_SHORT).show();
				}
				CommentsAdapter sadpt = new CommentsAdapter(data, Comments.this);
				lv.setAdapter(sadpt);
				lv.setOnItemLongClickListener(lvLongClick);
			}
		};
		handler.postDelayed(runnable, 500);
	}

	private AdapterView.OnItemLongClickListener lvLongClick = new AdapterView.OnItemLongClickListener() {
		public boolean onItemLongClick(final AdapterView<?> adpt, View view,
				final int potions, long id) {
			cadpt = (CommentsAdapter) adpt.getAdapter();
			bid = cadpt.getBlogId(potions);
			pid = cadpt.getItemId(potions);
			hide = cadpt.getHide(potions);
			
			Builder alert = new AlertDialog.Builder(context);
			alert.setTitle(R.string.ctrol);
			Resources rs = getResources();
			String[] its = new String[3];
			its[0] = rs.getString(R.string.delete);
			its[2] = rs.getString(R.string.rply);
			if(hide.equals("n")){
				its[1]= rs.getString(R.string.shield);
			}else{
				its[1]= rs.getString(R.string.verify);
			}
			alert.setItems(its, new DialogInterface.OnClickListener() {
				public void onClick(DialogInterface dialog, int which) {

					switch (which) {
					case 1:
						if(hide.equals("n")){
							beachComment("hidecom",adpt,potions);
						}else{
							beachComment("showcom",adpt,potions);
						}
						break;
					case 2:
						showReply();
						break;
					default:
						beachComment("delcom",adpt,potions);
						break;
					}
					/*
					 * ad变量用final关键字定义，因为在隐式实现的Runnable接口 的run()方法中 需要访问final变量。
					 * 
					 * final AlertDialog ad = new AlertDialog.Builder(context)
					 * .setMessage( "你选择的是：" + which + ": " + its[which])
					 * .show();
					 * 
					 * Handler handler = new Handler(); Runnable runnable = new
					 * Runnable() { public void run() { //
					 * 调用AlertDialog类的dismiss()方法关闭对话框，也可以调用cancel()方法。
					 * ad.dismiss();
					 * 
					 * } }; // 5秒后运行run()方法。 handler.postDelayed(runnable, 5 *
					 * 1000);
					 */

				}
			});
			alert.create().show();
			return false;
		}
	};
	
	private void showReply(){
		Builder alert = new AlertDialog.Builder(Comments.this);
		final EditText tucao = new EditText(Comments.this);
		tucao.setInputType(InputType.TYPE_TEXT_FLAG_MULTI_LINE);
		tucao.setSingleLine(false);
		tucao.setGravity(Gravity.TOP);
		tucao.setMaxLines(5);
		alert.setTitle(getResources().getString(R.string.rply));
		alert.setView(tucao);
		alert.setPositiveButton("确认", new OnClickListener() {
			public void onClick(DialogInterface dialog, int which) {
				Http http = new Http(Comments.this);
				HashMap<String, Object> parmas = new HashMap<String, Object>();
				String contents = tucao.getText().toString();
				parmas.put("c", contents);
				parmas.put("bid", bid);
				parmas.put("pid", pid);
				Result obj = http.post(Action.BLOG_COMMENT_RPL,parmas);
				if(obj.getStatus()){
					Toast.makeText(Comments.this, "回复成功！", Toast.LENGTH_SHORT).show();
				}else{
					Toast.makeText(Comments.this, obj.getData().toString(), Toast.LENGTH_SHORT).show();
				}
			}
		});
		alert.setNegativeButton("取消", null);
		alert.show();
	}
	
	@SuppressWarnings("unchecked")
	private void beachComment(String action,AdapterView<?> adpt,int po){
		Http http = new Http(Comments.this);
		HashMap<String, Object> parmas = new HashMap<String, Object>();
		parmas.put("ac", action);
		parmas.put("pid", pid);
		Result obj = http.post(Action.BLOG_COMMENT_BATCH,parmas);
		if(obj.getStatus()){
			Toast.makeText(Comments.this, "操作成功！", Toast.LENGTH_SHORT).show();
			if(action.equals("delcom")){
				data.remove(po);
			}else if(action.equals("hidecom")){
				JSONObject ob = (JSONObject) data.get(po);
				ob.put("hide", "y");
				data.add(po, ob);
			}else{
				JSONObject ob = (JSONObject) data.get(po);
				ob.put("hide", "n");
				data.add(po, ob);
			}
			cadpt.notifyDataSetChanged();
		}else{
			Toast.makeText(Comments.this, obj.getData().toString(), Toast.LENGTH_SHORT).show();
		}
	}
}
